package com.cherrish.demo;

import com.cherrish.demo.redis.MessageReceiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @author cherrish
 * @time 2018-08-30 14:51
 * @name RedisConfig
 * @desc:
 */
@Configuration
public class RedisConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter sendListenerAdapter, MessageListenerAdapter replyListenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(sendListenerAdapter, new PatternTopic("send"));
        container.addMessageListener(replyListenerAdapter, new PatternTopic("replay"));
        return container;
    }

    @Bean
    MessageListenerAdapter sendListenerAdapter(MessageReceiver receiver){
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    MessageListenerAdapter replyListenerAdapter(MessageReceiver receiver){
        return new MessageListenerAdapter(receiver, "replyMessage");
    }


    @Bean
    StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }
}
